package com.google.code.swingonfire.component.page;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Insets;
import java.awt.LayoutManager2;
import java.awt.Rectangle;
import java.io.Serializable;

import com.google.code.swingonfire.layout.LayoutUtils;
import com.google.code.swingonfire.utility.Utils;

class JPageComponentLayout implements LayoutManager2, Serializable
{

    private static final long serialVersionUID = -5294971266692152179L;

    public static final JPagedPaneLayout INSTANCE = new JPagedPaneLayout();

    protected JPageComponentLayout()
    {
        super();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addLayoutComponent(Component comp, Object constraints)
    {
        // intentionally left blank
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addLayoutComponent(String name, Component comp)
    {
        // intentionally left blank
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void removeLayoutComponent(Component comp)
    {
        // intentionally left blank
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension preferredLayoutSize(Container parent)
    {
        synchronized (parent.getTreeLock())
        {
            Dimension size = new Dimension();

            for (Component component : parent.getComponents())
            {
                LayoutUtils.maximum(size, component.getPreferredSize());
            }

            return LayoutUtils.addInsets(size, parent);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension minimumLayoutSize(Container parent)
    {
        synchronized (parent.getTreeLock())
        {
            Dimension size = new Dimension();

            for (Component component : parent.getComponents())
            {
                LayoutUtils.maximum(size, component.getMinimumSize());
            }

            return LayoutUtils.addInsets(size, parent);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Dimension maximumLayoutSize(Container parent)
    {
        synchronized (parent.getTreeLock())
        {
            Dimension size = new Dimension();

            for (Component component : parent.getComponents())
            {
                LayoutUtils.maximum(size, component.getMaximumSize());
            }

            return LayoutUtils.addInsets(size, parent);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void layoutContainer(Container parent)
    {
        for (Component component : parent.getComponents())
        {
            layoutComponent(parent, component);
        }
    }

    public boolean layoutComponent(Container parent, Component component)
    {
        Insets insets = parent.getInsets();
        Dimension size = parent.getSize();
        Rectangle bounds =
            new Rectangle(insets.left, insets.top, size.width - insets.left - insets.right, size.height - insets.top
                - insets.bottom);

        Rectangle originalBounds = component.getBounds();

        bounds = LayoutUtils.center(bounds, component);

        if (!Utils.equals(originalBounds, bounds))
        {
            component.setBounds(bounds);

            return true;
        }

        return false;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public float getLayoutAlignmentX(Container target)
    {
        return 0.5f;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public float getLayoutAlignmentY(Container target)
    {
        return 0.5f;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void invalidateLayout(Container target)
    {
        // intentionally left blank
    }

}
